home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / programming / e / lsestuff / multidim.e < prev    next >
Text File  |  1999-11-29  |  5KB  |  244 lines

  1. OPT MODULE
  2.  
  3. MODULE 'leifoo/nm'
  4. MODULE 'leifoo/nmIList'
  5.  
  6.  
  7.  
  8. ->this object is node and list at the same time
  9.  
  10. ->funkar äntligen!! :))) 991025 cooooolt..
  11.  
  12. ->nu funkar det inte..!! :) ..  gjort om lite..gör..
  13.  
  14. -> 991030 -> ny verr igen.. raderade det gamla..
  15.  
  16. ->ser bra ut.. bara addMD() remMD(), findMD(), travMD()..
  17.  
  18. ->explanation :
  19. /* one object that is node and list at the same time..
  20.    every object has an ID. there can be only ONE
  21.    object with a sertain ID at one certain 'depth-level'.
  22.    or : ChildObjects to a certain motherobject
  23.    must have different ID's.. the mdAdd() does
  24.    nothing if it finds an already existing object
  25.    the same ID..
  26.    M = mother
  27.    C = Child
  28.  
  29.    Inherit your Mother and Child Objects
  30.    from 'multidim'
  31.  
  32.       M
  33.       |
  34.       +--+
  35.      / \  \
  36.     C1 C2 C7    THIS IS OKEY
  37.     |  |    \
  38.     +  +     +--+
  39.    / \  \     \  \
  40.   C1 C5  C2   C2 C4
  41.  
  42.   To find C5 from mother (M) :
  43.   c5ptr := mother.mdFind([1, 5]) (remember the [] !!)
  44.  
  45.   To add a C25 to C5 :
  46.   c5ptr.mdAdd([25], NEW multidimptr)
  47.  
  48.   OR
  49.  
  50.   c5ptr.mdAdd([25], multidimobject)
  51.   (old multidimobject.id (if any)
  52.   gets overwritten with in this case, 25)
  53.  
  54.   OR
  55.  
  56.   mother.mdAdd([1, 5, 25], NEW c25ptr)
  57.  
  58.   NOTE! If u pass a path that doesnt exist
  59.   to mdAdd() the path will be created!
  60.   (nodes (OBJECT multidim) will be created to make the path)
  61.   ex : mother.mdAdd([1, 5, 25, 32, 89], multidimobject) WILL WORK!
  62.                      e  e  e   f   o
  63.   e = exists
  64.   f = fill
  65.   o = the object that gets added
  66.  
  67.  
  68.   To delete C5 :
  69.   END c5ptr
  70.  
  71.   OR
  72.  
  73.   mother.mdRem([1, 5])
  74.  
  75.   OR
  76.  
  77.   c5ptr := mother.mdFind([1, 5])
  78.   END c5ptr
  79.  
  80.   If u delete a node that has childs, the childs gets
  81.   removed also !!
  82.  
  83.   So a 'END mother' ends it all...
  84.  
  85.   There is also a special traverse method
  86.   that traverses every node beneth the node
  87.   u specify by the elist argument..
  88.   mdTrav([1], proc) would in this case
  89.   call your procedure 'proc' twice ;
  90.   for the C1 and C5 beneath M/C1..
  91.  
  92.   Prepare your proc for receiving
  93.   the multidim_travObj OBJECT..
  94.   The thischild field in it is a ptr
  95.   to the actual node..
  96.   the mother field is the mother of this node..
  97.  
  98.   If u want to give your proc some
  99.   own arguments, allocate your own
  100.   OBJECT inherited from multidim_travObj
  101.   with additional fields and pass it to
  102.   the traversemethod :
  103.   mdTrav(list of nmbrs, proc, mytravobject)
  104.  
  105.   bla.mdTrav([], proc) Traverses all nodes beneath
  106.   bla.
  107.  
  108.  
  109. */
  110.  
  111. /* inherit from this one */
  112. EXPORT OBJECT multidim OF nmIList
  113. ENDOBJECT
  114.  
  115. PROC getObjectName() OF multidim IS 'multidim'
  116.  
  117. PROC shrink_elist(elist:PTR TO LONG, len)
  118.    DEF a
  119.    len--
  120.    FOR a := 1 TO len DO elist[a-1] := elist[a]
  121.    SetList(elist, len - 1)
  122. ENDPROC
  123.  
  124. EXPORT OBJECT multidim_travObj
  125.    thischild:PTR TO multidim
  126.    mother:PTR TO multidim
  127. ENDOBJECT
  128.  
  129. EXPORT OBJECT mdv OF multidim
  130.    value
  131. ENDOBJECT
  132.  
  133. PROC getObjectName() OF mdv IS 'mdv'
  134.  
  135. PROC clone_elist(elist, len)
  136.    DEF c
  137.    c := List(len)
  138.    ListCopy(c, elist)
  139. ENDPROC c
  140.  
  141. PROC mdvSet(elist, value) OF mdv
  142.    DEF mdv:PTR TO mdv
  143.    mdv := self.mdFind(elist)
  144.    IF mdv = NIL THEN self.mdAdd(elist, NEW mdv)
  145.    IF StrCmp(mdv.getObjectName(), 'mdv') <> TRUE
  146.       self.replace(mdv, NEW mdv)
  147.    ENDIF
  148.    mdv.value := value
  149. ENDPROC
  150.  
  151. PROC mdvGet(elist) OF mdv
  152.    DEF mdv:PTR TO mdv
  153.    mdv := self.mdFind(elist)
  154. ENDPROC IF mdv THEN mdv.value ELSE NIL
  155.  
  156. PROC mdAdd(elist, md:PTR TO multidim) OF multidim
  157.    DEF elen
  158.    DEF newmd:PTR TO multidim
  159.    DEF elcopy:PTR TO LONG
  160.    elen := ListLen(elist)
  161.    elcopy := clone_elist(elist, elen)
  162.  
  163.    WHILE elen <> NIL
  164.       elen := ListLen(elcopy)
  165.       newmd := self.find(elcopy[])
  166.       IF newmd = NIL
  167.          IF elen = 1
  168.             md.id := elcopy[]
  169.             self.addLast(md)
  170.             self := md
  171.          ELSE
  172.             NEW newmd
  173.             newmd.id := elcopy[]
  174.             self.addLast(newmd)
  175.             self := newmd
  176.          ENDIF
  177.       ELSE
  178.          self := newmd
  179.       ENDIF
  180.       shrink_elist(elcopy, elen)
  181.    ENDWHILE
  182.  
  183.    DisposeLink(elcopy)
  184. ENDPROC self
  185.  
  186. PROC mdRem(elist) OF multidim
  187.    DEF md:PTR TO multidim
  188.    md := self.mdFind(elist)
  189.    IF md THEN END md
  190. ENDPROC
  191.  
  192. PROC mdFind(elist) OF multidim
  193.    DEF elen
  194.    DEF elcopy:PTR TO LONG
  195.    elen := ListLen(elist)
  196.    elcopy := clone_elist(elist, elen)
  197.  
  198.    WHILE elen <> NIL
  199.       elen := ListLen(elist)
  200.       self := self.find(elcopy[])
  201.       EXIT self = NIL
  202.       shrink_elist(elcopy, elen)
  203.    ENDWHILE
  204.  
  205.    DisposeLink(elcopy)
  206. ENDPROC self
  207.  
  208. PROC mdReplace(elist, md:PTR TO multidim) OF multidim
  209.    DEF oldmd:PTR TO multidim
  210.    oldmd := self.mdFind(elist)
  211.    IF oldmd = NIL THEN RETURN NIL
  212.    oldmd.replace(oldmd, md)
  213.    END oldmd
  214. ENDPROC md
  215.  
  216. PROC mdTrav(elist, proc, obj=NIL) OF multidim
  217.    DEF md:PTR TO multidim
  218.    DEF mto:PTR TO multidim_travObj
  219.    IF obj = NIL THEN NEW mto ELSE mto := obj
  220.    mto.mother := self
  221.  
  222.    IF ListLen(elist) > 0 THEN md := self.mdFind(elist) ELSE md := self
  223.  
  224.    IF md = NIL
  225.       END mto
  226.       RETURN NIL
  227.    ENDIF
  228.  
  229.    md := md.first()
  230.  
  231.    WHILE md
  232.       mto.thischild := md
  233.       proc(mto)
  234.       md.mdTrav([], proc, mto)
  235.       md := md.next
  236.    ENDWHILE
  237.  
  238.    IF obj = NIL THEN END mto
  239. ENDPROC TRUE
  240.  
  241.  
  242.  
  243.  
  244.